<?php
namespace App\Models;

use App\Models\CommentM;
use DB;
use Illuminate\Database\Eloquent\Model;

class ReplyM extends Model
{
    static public $STable="lee_comment_reply";
    protected  $table="lee_comment_reply";
    
    /**
     * 添加 留言 回复记录
     * @param bigint $commentid
     * @param array $data 数据项
     * 如 ['rpcontent'=>'','userid'=>12,
     *   'rp_userid'=>90,'commentid'=>4548]
     */
    static public function addItem(array $data){
        $replyInst=new ReplyM();
        foreach ($data as $key => $value){
            $replyInst->$key=$value;
        }
        // 被赞数 0
        // $replyInst->beliked=0;
        $replyInst->save();
        return $replyInst->id;
    }
    
    /**
     * 删除 回复 记录
     * @param unknown $rpid
     */
    static public function dltItem($rpid){
        self::where('id',$rpid)->delete();
    }
    
    /**
     * 依据comment的id 返回限制记录和偏移量的记录
     * @param int $cmtid，comment的id
     * @param int $lmt， 限制记录数目
     * @param int $offset，检索结果偏移量
     * 
     * rpid（reply的id）,rpcontent（回复内容）,
     * rp_userid（回复者id）,be_rp_userid（被回复者id）,createdat（创建时间）
     */
    static public function loadRps($cmtid,$lmt,$offset){
            $rpT=self::$STable;
            $sql="select rp.id as rpid, rp.rpcontent as rpcontent,rp.userid as rp_userid,
            rp.rp_userid as be_rp_userid ,rp.updated_at as createdat
            from $rpT as rp
            where rp.commentid=$cmtid limit $lmt offset $offset";
            return DB::select($sql);
    }
    
    /**
     * 根据comment的id 删除数据记录
     * @param bigint $cmtid,comment's id
     */
    static public function deltRpsByCmtID($cmtid){
        self::where('commentid',$cmtid)->delete();
    }
    
    /**
     * 依据 comment 的id集合 删除相应的数据记录
     * @param array $cmtids，comment's ids
     * 二维数组 [cmtid1,cmtid2,cmtid3]
     */
    static public function deltItemsByCmtIDs(array $cmtids){
        self::whereIn('commentid',$cmtids)->delete();
    }
    
    /**
     * 依据iocn的fid值 返回 该icon的  回复数
     * @param bigint $iconfid icon的fid值
     * @return int 该icon评论的回复数
     */
    static public function singleCount($iconfid){
        $commentT=CommentM::$STable;
        $rpTable=self::$STable;
        $sql="select cm.iconfid as icnfid ,count(rp.id) as rpnum
            from $rpTable as rp
            left outer join $commentT   as cm
            on rp.commentid=cm.id
            where cm.iconfid=$iconfid
            group by cm.iconfid";
        return DB::select($sql);
    }
    
    /**
     * 依据icon的fid集合返回 其 对应的评论回复数
     * @param array $iconfids icon的fid的集合 [icnfid1,icnfid2]
     * 返回形式 : [  
     *  ['icnfid'=>icnfid1,'rpnum'=>rpnum1],
     *  ['icnfid'=>icnfid2,'rpnum'=>rpnum2],
     *  ['icnfid'=>icnfid3,'rpnum'=>rpnum3],
     * ]
     */
    static public function multiCount(array $iconfids){
        $iconfidStr=implode(',', $iconfids);
        $commentT=CommentM::$STable;
        $rpTable=self::$STable;
        $sql="select cm.iconfid as icnfid ,count(rp.id) as rpnum
            from $rpTable as rp
            left outer join $commentT   as cm
            on rp.commentid=cm.id
            where cm.iconfid in ( $iconfidStr )
            group by cm.iconfid";
        return DB::select($sql);
        
    }
    
    

}

?>